package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName DictController
 * @Description 此类描述:
 * @Author dangchen
 * @DateTime 2022-10-09 19:45
 * @Version 1.0
 */
@Api("数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {
    @Autowired
    private DictService dictService;

    //根据数据id查询子数据列表
    @ApiOperation("根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public R findChildData(@PathVariable Long id){
        List<Dict> list = dictService.findChildData(id);
        return R.ok().data("list",list);
    }

    @ApiOperation("导出")
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response){
        dictService.exportData(response);
    }

    @ApiOperation("导入")
    @PostMapping("importData")
    public R importDictData(MultipartFile file){
        dictService.importDictData(file);
        return R.ok();
    }

    // http://localhost:8202/admin/cmn/dict/getName/Hostype/1    三级甲等
    @GetMapping("/getName/{parentDictCode}/{value}")
    public String getName(@PathVariable String parentDictCode,@PathVariable String value){
        return dictService.getNameByParentDictCodeAndValue(parentDictCode, value);
    }

    //// http://localhost:8202/admin/cmn/dict/getName/130100      石家庄市
    @GetMapping("/getName/{value}")
    public String getName(@PathVariable String value){
        return dictService.getNameByParentDictCodeAndValue("",value);
    }

    @GetMapping("/findByDictCode/{dictCode}")
    public R findByDictCode(@PathVariable String dictCode){
        List<Dict> list = dictService.findByDictCode(dictCode);
        return R.ok().data("list",list);
    }
}
